package com.android.tiango.baba.act;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.UserEntity;
import com.android.tiango.baba.entity.UserModel;
import com.android.tiango.baba.util.CommUtil;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;

public class ActRegister extends BaBaseActivity implements OnClickListener {

	//private ImageView mFork;
	private ImageView mDelete;
	private EditText mPhone;// login 的手机号码账号和密码
	private EditText mPassword;
	private ImageView mIsOpenPassword;// 控制密码是否显示为***
	private LinearLayout mEnterLL;
	private ImageView mPhone_icon;// 手机和密码锁小图标
	private ImageView mLock_icon;
	private Button mCode, mRegister;
	private EditText mCodeEdit;
	private TextView mCodeTime;
	private boolean isCodeTime;
	private boolean isCheck = false;

	private ImageView mCheck;// 判断是否阅读用户手册

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.layout_register);

		intiView();
	}

	private void intiView() {
		// TODO Auto-generated method stub
		setTitleName("快速注册");
		mTitleBar.setBackIcon(R.drawable.back);
		setBackOnClickListener();

		//mFork = (ImageView) findViewById(R.id.iv_fork_delete);
		mCode = (Button) findViewById(R.id.bt_code);
		mCodeEdit = (EditText) findViewById(R.id.et_code);
		mRegister = (Button) findViewById(R.id.bt_register);
		mCodeEdit.setInputType(EditorInfo.TYPE_CLASS_PHONE);// 弹出数字输入框
		mCheck = (ImageView) findViewById(R.id.iv_usercode);

		mCodeTime = (TextView) findViewById(R.id.tv_code);
		mPhone = (EditText) findViewById(R.id.ic_enter).findViewById(
				R.id.et_phone);
		mPassword = (EditText) findViewById(R.id.ic_enter).findViewById(
				R.id.et_password);
		mDelete = (ImageView) findViewById(R.id.ic_enter).findViewById(
				R.id.iv_phone_delete);
		mIsOpenPassword = (ImageView) findViewById(R.id.ic_enter).findViewById(
				R.id.iv_passwordclose);
		mEnterLL = (LinearLayout) findViewById(R.id.ic_enter).findViewById(
				R.id.ll_enter);
		mPhone_icon = (ImageView) findViewById(R.id.ic_enter).findViewById(
				R.id.iv_phone);
		mLock_icon = (ImageView) findViewById(R.id.ic_enter).findViewById(
				R.id.iv_lock);

		mPhone.setInputType(EditorInfo.TYPE_CLASS_PHONE);// 弹出数字输入框
		
		mIsOpenPassword.setImageResource(R.drawable.password_open2);
		mIsOpenPassword.setOnClickListener(this);
		
//		mPhone_icon.setBackgroundResource(R.drawable.phone2);
//		mLock_icon.setBackgroundResource(R.drawable.lock2);
//		mEnterLL.setBackgroundResource(R.drawable.frame_enter2);

		mCheck.setImageResource(R.drawable.icon_check2);
		isCheck = true;
		//mFork.setOnClickListener(this);
		mCode.setOnClickListener(this);
		mRegister.setOnClickListener(this);
		mCheck.setOnClickListener(this);
		mCodeTime.setText("60");
		String phoneNumber=ShareCookie.getUserAccount();
		mPhone.setText(phoneNumber);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		/*case R.id.iv_fork_delete:
			finish();
			break;*/
		case R.id.bt_code:
			String phone1=mPhone.getText().toString();
			if (CommUtil.isMobileNO(phone1)) {
				if (isCodeTime == false) {
					showWaitingDialog();
					onLoadCode();
				} else {
					ToastUtils.showMessage("请稍后点击");
				}
			} else {
				ToastUtils.showMessage("请输入正确手机号码");
			}
			break;
		case R.id.bt_register:
			if (CommUtil.isNetworkAvailable(this)) {
				if (isCheck) {
					String phone = mPhone.getText().toString().trim();
					String pass = mPassword.getText().toString().trim();
					String code=mCodeEdit.getText().toString().trim();
					if(StringUtils.isEmpty(phone)){
						ToastUtils.showMessage("手机号码不能为空！！");
						return;
					}
					if(StringUtils.isEmpty(pass)){
						ToastUtils.showMessage("密码不能为空！！");
						return;
					}
					if(StringUtils.isEmpty(code)){
						ToastUtils.showMessage("验证码不能为空！！");
						return;
					}
					if(CommUtil.isMobileNO(phone)){
						showWaitingDialog();
						onLoadData();
					}else{
						ToastUtils.showMessage("请输入正确手机号码");
						return;
					}
				} else {
					ToastUtils.showMessage("请阅读用户手册！");
					return;
				}
			} else {
				ToastUtils.showMessage("没有网络连接");
				return;
			}
			break;
		case R.id.iv_usercode:
			if (isCheck) {
				mCheck.setImageResource(R.drawable.icon_uncheck);
				isCheck = false;
			} else {
				mCheck.setImageResource(R.drawable.icon_check);
				isCheck = true;
			}
			break;
		default:
			break;
		}
	}

	private void onLoadCode() {
		// TODO Auto-generated method stub
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams httpparams = new HttpParams();
		httpparams.put("userName", mPhone.getText().toString());
		httpparams.put("sendType", "0");

		client.post(HttpUrl.getUrl(HttpUrl.CODE), httpparams,
				new HttpCallBack() {

					@Override
					public void onHttpSuccess(Object arg0) {
						// TODO Auto-generated method stub
						ToastUtils.showMessage("验证码获取成功！");
						dismissWaitingDialog();
						final long time1 = System.currentTimeMillis();
						isCodeTime = true;
						new Thread(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								while (isCodeTime) {

									try {
										Thread.sleep(1000);
										long time2 = System.currentTimeMillis();

										Message message = new Message();
										message.what = 0203;
										message.arg1 = (int) (time2 - time1);
										handler.sendMessage(message);

									} catch (InterruptedException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}
						}).start();
					}

					@Override
					public void onHttpStarted() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						ToastUtils.showMessage("验证码获取失败！===" + arg1);

						arg0.printStackTrace();
					}
				});
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0203:
				if (60 - (msg.arg1 / 1000) < 0) {
					isCodeTime = false;
					mCode.setTextColor(Color.WHITE);
				} else {
					mCodeTime.setText(60 - (int) (msg.arg1 / 1000) + "");
					mCode.setTextColor(Color.GRAY);
				}

				break;

			default:
				break;
			}
		};
	};

	@Override
	protected void onDestroy() {
		isCodeTime = false;
		super.onDestroy();

	};

	/**
	 * 
	 */
	private void onLoadData() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		client.setPrintLog(true);
		HttpParams httpparams = new HttpParams();
		httpparams.put("userName", mPhone.getText().toString());
		httpparams.put("password", mPassword.getText().toString());
		httpparams.put("captcha", mCodeEdit.getText().toString());
		client.post(HttpUrl.getUrl(HttpUrl.REGISTER), httpparams,
				new HttpCallBack() {

					@Override
					public void onHttpSuccess(Object arg0) {
						dismissWaitingDialog();
						UserEntity entity = (UserEntity) arg0;
						UserModel data = entity.getData();
						if (null != data) {
							ShareCookie.saveUserInfo(data);
							showActivity(ActHome.class, true);
						}
						ToastUtils.showMessage("注册成功！");

					}

					@Override
					public void onHttpStarted() {

					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						ToastUtils.showMessage("注册失败！===" + arg1);
						arg0.printStackTrace();
					}
				}, UserEntity.class);//
	}

	private void request() {

	}
}
